import json

import sql
from service import MasterService
from websocket_information import WebSocketController


def get_connections(ws, uuid):
    try:
        devices = sql.show_device()
        dlist = []
        for d in devices:
            dlist.append((d[0], d[1]))
        devices_dict = dict(dlist)

        groups = sql.show_groups()
        glist = []
        for g in groups:
            glist.append((g[0], g[1]))
        groups_dict = dict(glist)
        connections = sql.show_connection()
        print(devices_dict)
        connections_list = []
        for c in connections:
            print(c)
            device_name = devices_dict[c[1]]
            related_device_name = None
            related_group_name = None
            if c[3] is not None:
                related_device_name = devices_dict[c[3]]
            elif c[4] is not None:
                related_group_name = groups_dict[c[4]]
            connection_info = {'connection_id': c[0], 'device_id': c[1], 'device_name': device_name,
                               'related_type': c[2],
                               'related_device_id': c[3], 'related_device_name': related_device_name,
                               'related_group_id': c[4], 'related_group_name': related_group_name}
            connections_list.append(connection_info)
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        data['message'] = connections_list
        article = json.dumps(data, ensure_ascii=False)
        print(article)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def get_connection_by_id(ws, uuid, message):
    try:
        devices = sql.show_device()
        dlist = []
        for d in devices:
            dlist.append((d[0], d[1]))
        devices_dict = dict(dlist)
        c = sql.get_connection_info(message)
        device_name = devices_dict[c[1]]
        related_device_name = None
        related_group_name = None
        if c[3] is not None:
            related_device_name = devices_dict[c[3]]
        elif c[4] is not None:
            related_group_name = devices_dict[c[4]]
        connection_info = {'connection_id': c[0], 'device_id': c[1], 'device_name': device_name,
                           'related_type': c[2],
                           'related_device_id': c[3], 'related_device_name': related_device_name,
                           'related_group_id': c[4], 'related_group_name': related_group_name}

        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        data['message'] = connection_info
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def add_connection(ws, uuid, message):
    try:
        device_id = message['device_id']
        related_type = message['related_type']
        related_id = message['related_id']

        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        res = sql.add_connection(device_id, related_type, related_id)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def update_connection(ws, uuid, message):
    try:
        c_id = message['connection_id']
        device_id = message['device_id']
        related_type = message['related_type']
        related_id = message['related_id']

        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        res = sql.modify_connection(c_id, device_id, related_type, related_id)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def delete_connection(ws, uuid, message):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        res = sql.delete_connection(message)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def get_relational_brain_map(ws, uuid):
    try:
        brain_map_data = MasterService.get_relational_brainMap()
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        data['message'] = brain_map_data
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def get_logs(ws, uuid, message):
    search_text = message['searchText']
    page_index = message['pageIndex']
    page_size = message['pageSize']
    execution_logs = sql.show_executionLog_paging(search_text, page_index, page_size)
    print(execution_logs)

    article_info = {}
    data = json.loads(json.dumps(article_info))
    data['uuid'] = uuid
    data['message'] = execution_logs
    article = json.dumps(data, ensure_ascii=False)
    print(article)
    WebSocketController.on_send(ws, article)
